package com.activity.drag.drop;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DragDropAdapter extends BaseAdapter implements RemoveListener, DropListener{ 

    private LayoutInflater mInflater;
    private ArrayList<String> mContent;
	@SuppressWarnings("rawtypes")
	private ArrayList aContents;
    ArrayList<String> leftContents = null;
	ArrayList<String> rightContents = null;
	ArrayList<Integer> images;
	HashMap<String, String> answers = null;
    
    @SuppressWarnings("rawtypes")
	public DragDropAdapter(Context context, int[] itemLayouts, int[] itemIDs, ArrayList content) {
    	initi(context,itemLayouts,itemIDs, content);
    }

    @SuppressWarnings("unchecked")
	private void initi(Context context, int[] layouts, int[] ids, ArrayList allcontent) {
    	// Cache the LayoutInflate to avoid asking for a new one each time.
    	mInflater = LayoutInflater.from(context);
    	aContents = allcontent;
    	if(aContents!= null && !aContents.isEmpty() && aContents.size()== 4){
    		leftContents = (ArrayList<String>)aContents.get(0);
    		rightContents = (ArrayList<String>)aContents.get(1);
    		images = (ArrayList<Integer>) aContents.get(2);
    		answers = (HashMap<String, String>)aContents.get(3);
    	}
    }
    
	
  // gets the number of items in the list
    public int getCount() {
       // return mContent.size();
    	int retVal = -1;
    	if(leftContents!=null && !leftContents.isEmpty()  && rightContents!=null &&! rightContents.isEmpty()){
    		if(leftContents.size()>rightContents.size())
    			retVal = leftContents.size();
    		else 
    			retVal = rightContents.size();
    	}
    	return retVal;
    }

    //Gets the item from rightContents
    public String getItem(int position) {
        return rightContents.get(position);
    }

    //get the position
    public long getItemId(int position) {
        return position;
    }

    // Make a view to hold each row.
    public View getView(int position, View convertView, ViewGroup parent) {

		View rowView = mInflater.inflate(R.layout.items, parent, false);
		TextView textView = (TextView) rowView.findViewById(R.id.TextView01);
		TextView textView2 = (TextView) rowView.findViewById(R.id.TextView02);
		ImageView imageView = (ImageView) rowView.findViewById(R.id.ImageView01);

		if(position<leftContents.size()) {
			textView2.setText(leftContents.get(position));
		}
		if(position<rightContents.size()) {
			textView.setText(rightContents.get(position));
		}
		imageView.setImageResource(images.get(position));

		return rowView;
    }

	public void onRemove(int which) {
		if (which < 0 || which > rightContents.size()) return;		
		rightContents.remove(which);
	}

	public void onDrop(int from, int to) {
		String temp = rightContents.get(from);
		rightContents.remove(from);
		rightContents.add(to,temp);
	}

}
